package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallOrderGoodsMapper;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsExample;
import org.linlinjava.litemall.db.util.OrderGoodsConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallOrderGoodsService {
    @Resource
    private LitemallOrderGoodsMapper orderGoodsMapper;

    public int add(LitemallOrderGoods orderGoods) {
        orderGoods.setAddTime(LocalDateTime.now());
        orderGoods.setUpdateTime(LocalDateTime.now());
        return orderGoodsMapper.insertSelective(orderGoods);
    }

    /**
     * 根据orderID  去orderGoods 找到 订单中的的商品(list)...
     * @param orderId
     * @return
     */
    public List<LitemallOrderGoods> queryByOid(Integer orderId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return orderGoodsMapper.selectByExample(example);
    }

    /**
     * 根据orderID 去orderGoods 查goodsId..获取几个商品的总价
     * @param orderId
     * @return
     */
    public BigDecimal orderAllGoodsPrice(Integer orderId){
        List<LitemallOrderGoods> litemallOrderGoods = queryByOid(orderId);
        BigDecimal totalPrice = new BigDecimal(0);
        for (LitemallOrderGoods orderGoods : litemallOrderGoods) {
            //GoodsPriceOrigin是商品原价   不是GoodsPrice
            BigDecimal goodsSinglePriceOrigin = orderGoods.getGoodsPriceOrigin();
            Short number = orderGoods.getNumber();
            BigDecimal goodsPricetotal = goodsSinglePriceOrigin.multiply(new BigDecimal(number));
            totalPrice= totalPrice.add(goodsPricetotal);
        }
        System.out.println("商品原价的总价totalPrice>>>" + totalPrice);
        return totalPrice;
    }

    public List<LitemallOrderGoods> queryByStateAndOid(Integer orderId, Short state) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andStateEqualTo(state).andOrderIdEqualTo(orderId);
        return orderGoodsMapper.selectByExample(example);
    }

    public List<LitemallOrderGoods> findByOidAndGid(Integer orderId, Integer goodsId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return orderGoodsMapper.selectByExample(example);
    }

    public LitemallOrderGoods findById(Integer id) {
        return orderGoodsMapper.selectByPrimaryKey(id);
    }

    public void updateById(LitemallOrderGoods orderGoods) {
        orderGoods.setUpdateTime(LocalDateTime.now());
        orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);
    }

    public Short getComments(Integer orderId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        long count = orderGoodsMapper.countByExample(example);
        return (short) count;
    }

    public void completeOrder(Integer orderId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andOrderIdEqualTo(orderId).andStateEqualTo(OrderGoodsConstant.NORMAL);
        LitemallOrderGoods goods = new LitemallOrderGoods();
        goods.setState(OrderGoodsConstant.COMPLETE);
        orderGoodsMapper.updateByExampleSelective(goods, example);
    }

    public boolean checkExist(Integer goodsId) {
        LitemallOrderGoodsExample example = new LitemallOrderGoodsExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return orderGoodsMapper.countByExample(example) != 0;
    }
}
